/**
 * 
 */
package com.thon.schedule;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.thon.dao.user.UserSettingDao;
import com.thon.entity.project.Project;
import com.thon.entity.project.ProjectAttention;
import com.thon.entity.user.Coupon;
import com.thon.entity.user.User;
import com.thon.service.project.ProjectAttentionService;
import com.thon.service.project.ProjectService;
import com.thon.service.user.CouponService;
import com.thon.service.user.MessageService;
import com.thon.service.user.UserService;
import com.thon.service.user.UserSettingService;

/**
* @File: ProjectStatusSchedule.java
* @author zemo 
* @mail chenzeming@lanbaoo.com
* @date: Feb 19, 2014 11:04:32 PM
* @Description: 项目状态定时任务
 */
@Component
public class ProjectStatusSchedule{
	
	@Autowired
	private UserService userService;
	@Autowired
	private UserSettingService userSettingService;
	@Autowired
	private ProjectService projectService;
	@Autowired
	private MessageService messageService;
	@Autowired
	private ProjectAttentionService projectAttentionService;
	@Autowired
	private CouponService couponService;
	
	private static final Log log = LogFactory.getLog(ProjectStatusSchedule.class);
	//
	/**
	 * @Title: chackProjectRemainingDays
	 * @Description: 检查所有众筹(status==3)项目是否到期,如果到期转换状态执行中或失败
	 * 				检查执行众状态的执行计划日期是否截至，截止日期达到转换完结
	 * @return: void
	 */
	public void chackProjectRemainingDays(){
		List<Project> projects = new ArrayList<Project>();
		projects = projectService.listProjectByStatus(3);
		User from = new User();
		from = userService.getUserByEmail("service@jishanjia.com");//
		for (Project project : projects) {
			if(project.getRemainingDay() <= 0){
				if (project.getFundCurrent() >= project.getFundTarget()) {
					project.setStatus(4);
					//messageService.sendMessage(from, project.getInstitution().getUser(), "恭喜您，您的活动：《"+project.getName()+"》筹资成功！");
					List<ProjectAttention> attentions = projectAttentionService.listAttentionByPro(project);
					for (ProjectAttention projectAttention : attentions) {
						Integer uid=projectAttention.getUserId();
						if (userSettingService.SettingStatus(uid, "myAttention")) {
							User to = userService.getUser(uid);
							messageService.sendMessage(from, to, "恭喜您，您关注的活动：《"+projectAttention.getProject().getName()+"》筹资成功！感谢您的关注。");
						}
					}
					log.debug("change project "+project.getId()+project.getName()+" status to 4");
				}else {
					project.setStatus(6);
					//messageService.sendMessage(from, project.getInstitution().getUser(), "很抱歉，您的活动：《"+project.getName()+"》筹资失败！");
					List<ProjectAttention> attentions = projectAttentionService.listAttentionByPro(project);
					for (ProjectAttention projectAttention : attentions) {
						Integer uid=projectAttention.getUserId();
						if (userSettingService.SettingStatus(uid, "myAttention")) {
							User to = userService.getUser(uid);
							messageService.sendMessage(from, to, "很抱歉，您关注的活动：《"+projectAttention.getProject().getName()+"》筹资失败！感谢您的关注。");
						}
					}
					log.debug("change project "+project.getId()+project.getName()+" status to 6");
				}
				projectService.updateProject(project);
			}
		}
		
	}
	
	public void checkCoupon(){
		Date today = new Date();
		List<Coupon> couponList = couponService.listByStatus("0");
		if (couponList.size() > 0) {
			for(Coupon c : couponList) {
				if (c.getOverdueDate().before(today)) {
					c.setCouponStatus("2");
					couponService.update(c);
				}
			}
		}
	}
	
}
